<template>
  <div class="login-container">
    <!-- 导航栏区域 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="arrow-left" @click="$router.back()" />
    </van-nav-bar>
    <!-- 登录表单区域 -->
    <van-form ref="loginForm" @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        type="number"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <!-- 倒计时区域 time是倒计时时间 -->
          <van-count-down
            v-if="isCountDownShow"
            :time="timer"
            format="ss s"
            @finish="isCountDownShow = false"
          />
          <!-- 验证码区域 -->
          <van-button
            v-else
            class="send-sms-btn"
            size="small"
            type="default"
            native-type="button"
            @click="onsendSms"
            >获取验证码</van-button
          >
        </template>
      </van-field>
      <!-- 按钮区域 -->
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
export default {
  name: '',
  components: {},
  props: {},
  data() {
    return {
      user: {
        mobile: '13911111111', //手机号
        code: '246810', //验证码
      },
      userFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '手机号格式错误' },
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' },
        ],
      },
      // 是否展示倒计时
      isCountDownShow: false,
      // 设置倒计时数据为60秒
      timer: 1000 * 60,
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 登录的事件
    async onSubmit() {
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true, //禁用背景点击
        duration: 0, //持续时间默认2000，值为 0 时，toast 不会消失
      })
      try {
        const { data: res } = await login(this.user)
        this.$store.commit('setUser', res.data)
        this.$toast.success('登录成功')
        // 登录成功之后跳转到我的页面
        this.$router.back()
        // console.log(res)
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码错误')
        } else {
          this.$toast.fail('登录失败，请稍后重试')
        }
      }
    },
    // 获取验证码的事件
    async onsendSms() {
      // 1.校验手机号码
      try {
        // $refs 得到表单组件实例
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return '验证失败'
      }

      // 2.验证通过显示倒计时
      this.isCountDownShow = true
      // 3.请求发送验证码
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        // 发送失败关闭倒计时
        // this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    },
  },
}
</script>

<style lang="less">
.login-container {
  .toutiao {
    font-size: 25px;
  }
  .send-sms-btn {
    background-color: #3296fa;
    width: 80px;
    height: 24px;
    line-height: 24px;
    color: #fff;
    border-radius: 5px;
  }
  .login-btn-wrap {
    padding: 20px 15px;
  }
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}
</style>
